package com.example.sun.myapplication.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sun.myapplication.GetLightDeviceStateBean;
import com.example.sun.myapplication.R;

import java.util.zip.Inflater;

public class LoginActivity extends Activity {

    public static final String TAG = LoginActivity.class.getSimpleName();

    ImageView ivBack;

    TextView tvLoginBtn;
    TextView tvCountryValue;

    ImageView ivDeletePhone;
    ImageView ivDeletePassword;

    EditText etPhoneValue;
    EditText etPasswordValue;

    // 页面初始化时调用，如果页面没销毁，再次显示该页面时不会再次调用此方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w(TAG, "onCreate()");

        // 加载布局
        setContentView(R.layout.activity_login);

        ivBack = findViewById(R.id.iv_back);
        etPhoneValue = findViewById(R.id.et_phone_value);
        etPasswordValue = findViewById(R.id.et_password_value);
        ivDeletePhone = findViewById(R.id.iv_delete_phone);
        ivDeletePassword = findViewById(R.id.iv_delete_password);
        tvLoginBtn = findViewById(R.id.tv_login_btn);
        tvCountryValue = findViewById(R.id.tv_login_country_value);

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // TextView相关属性
        tvCountryValue.setText("美国");
        tvCountryValue.setText(R.string.usa);

        // 设置TextView的字体颜色
        tvCountryValue.setTextColor(Color.rgb(0xFF, 0x00, 0x00));
        int color = getResources().getColor(R.color.black);
        tvCountryValue.setTextColor(color);

        // 设置TextView字体大小(不需要乘系数)
//        float size = getResources().getDisplayMetrics().density * 16;
        tvCountryValue.setTextSize(16f);

        // 设置TextView背景色
        tvCountryValue.setBackgroundColor(color);
//        tvCountryValue.setBackgroundResource(R.drawable.adapter_operate_item_shape);

        tvLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(LoginActivity.this, "暂不支持的功能", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this, WeChatActivity.class);
                startActivity(intent);
            }
        });

        // ImageView相关属性
        // 设置前景图片（与布局src属性相同）
        ivDeletePhone.setImageResource(R.mipmap.delete_text);

        // 将图片转化为Bitmap显示
//        ivDeletePhone.setImageBitmap();
        // 设置背景图(与background属性相同)
//        ivDeletePhone.setBackgroundResource(R.mipmap.delete_text);

        ivDeletePhone.setVisibility(View.INVISIBLE);

        // EditText相关
        etPhoneValue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivDeletePhone.setVisibility(View.VISIBLE);
                } else {
                    ivDeletePhone.setVisibility(View.INVISIBLE);
                }
            }
        });

        etPasswordValue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    ivDeletePassword.setVisibility(View.VISIBLE);
                } else {
                    ivDeletePassword.setVisibility(View.INVISIBLE);
                }
            }
        });

        // 删除手机号
        ivDeletePhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etPhoneValue.setText("");
            }
        });

        // 删除密码文本
        ivDeletePassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etPasswordValue.setText("");
            }
        });

        GetLightDeviceStateBean bean = (GetLightDeviceStateBean) getIntent().getSerializableExtra("bean");
        String userName = getIntent().getStringExtra("userName");

        Log.v("verbose", "verbose log");
        Log.d("debug", "debug log");
        Log.i("info", "info log");
        Log.w("warn", "warn log");
        Log.e("error", "error log");
    }

    // 页面显示（处于栈顶）时会调用此方法
    @Override
    protected void onResume() {
        super.onResume();
        Log.w(TAG, "onResume()");
        tvCountryValue.setText("中国");
//        finish();
    }

    // 页面即将隐藏时（处于非栈顶）时会调用此方法
    @Override
    protected void onPause() {
        Log.w(TAG, "onPause()");
        super.onPause();

    }

    // 页面即将销毁时调用此方法
    @Override
    protected void onDestroy() {
        Log.w(TAG, "onDestroy()");
        super.onDestroy();

    }

}
